package com.jimikongjian.controller;

import com.jimikongjian.models.Message;
import com.jimikongjian.models.ResponseMessage;
import com.jimikongjian.models.Room;
import com.jimikongjian.service.base.IRoomService;
import com.jimikongjian.service.message.ResponseMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by fuxiuyang on 17/2/26.
 * 控制空间的操作
 */
@RestController("roomController")
@RequestMapping(value = "/v1")
public class RoomController {

    @Autowired
    @Qualifier("roomService")
    private IRoomService roomService;

    /**
     * 接受创建空间的接口
     * @param map
     * @return
     */
    @RequestMapping(value = "/room", method = RequestMethod.POST)
    @ResponseBody
    public ResponseMessage createRoom(@RequestBody Map<String,String> map){
        Room room = new Room();
        room.setPeoples(1);

        if(map.containsKey("name")){
            room.setName(map.get("name"));
            System.out.println("name is :" + room.getName());
        }
        if (map.containsKey("cover")){
            room.setCover(map.get("cover"));
            System.out.println("cover is :" + room.getCover());
        }
        if(map.containsKey("creatorId")){
            if (map.get("creatorId") != null){
                room.setCreatorId(Integer.parseInt(map.get("creatorId")));
                System.out.println("creatorId is :" + room.getCreatorId());
            }
        }
        if (map.containsKey("creatorEmail")){
            room.setCreatorEmail(map.get("creatorEmail"));
            System.out.println("creatorEmail is :" + room.getCreatorEmail());
        }
        if (map.containsKey("creatorNick")){
            room.setCreatorNick(map.get("creatorNick"));
            System.out.println("creatorNick is :" + room.getCreatorNick());
        }
        if (map.containsKey("signature")){
            room.setSignature(map.get("signature"));
            System.out.println("signature is :" + room.getSignature());
        }
        if (map.containsKey("type")){
            if (map.get("type") != null){
                room.setType(Integer.parseInt(map.get("type")));
                System.out.println("type is :" + room.getType());
            }
        }
        if (map.containsKey("peoples")){
            if (map.get("peoples") != null){
                room.setPeoples(Integer.parseInt(map.get("peoples")));
                System.out.println("peoples is :" + room.getPeoples());
            }
        }

        return  roomService.createRoom(room);
    }


    /**
     * 根据相应的信息，查询相对应的空间信息
     * @param start
     * @param length
     * @param type
     * @param id
     * @param userId
     * @param creatorId
     * @param name
     * @return
     **/
    @RequestMapping(value = "/room", method = RequestMethod.GET)
    @ResponseBody
    public ResponseMessage selectRoom(@RequestParam(name = "start", defaultValue = "0") Integer start,
                                      @RequestParam(name = "length", defaultValue = "15") Integer length,
                                      @RequestParam(name = "type", defaultValue = "-2") Integer type,
                                      @RequestParam(name = "id", defaultValue = "0") Integer id,
                                      @RequestParam(name = "userId", defaultValue = "0") Integer userId,
                                      @RequestParam(name = "creatorId", defaultValue = "0") Integer creatorId,
                                      @RequestParam(name = "name", defaultValue = "") String name){
        if (id != null && id != 0){
            return roomService.selectRoomById(id);
        }
        if (userId != null && userId != 0){
            return roomService.selectRoomByUserId(userId, type, start, length);
        }
        if (creatorId != null && creatorId != 0){
            return roomService.selectRoomByCreatorId(creatorId, type, start, length);
        }
        if (name != null && !name.equals("")){
            return roomService.selectRoomByName(name, type, start, length);
        }
        return roomService.selectRoom(start, length);
    }

    /**
     * 根据空间的id号，修改空间的信息
     * @param room
     * @return
     */
    @RequestMapping(value = "/room", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseMessage updateRoom(@RequestBody Room room){

        return roomService.updateRoom(room);
    }


}
